import Vue from "vue";
import VueRouter from "vue-router";
import index from "@/views/PageRouting/index.vue";

Vue.use(VueRouter);

// 异常捕获
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) {
    return originalPush.call(this, location, resolve, reject);
  }
  return originalPush.call(this, location).catch((e) => {});
};

// 相对路由 /index/home +/是相对路径
// 绝对路由 /home  没加是绝对路径
const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home.vue"),
    redirect: "/index", // 路由重定向
    children: [
      {
        path: "/index",
        name: "index",
        component: index,
      },
      {
        path: "/classify",
        name: "classify",
        component: () => import("../views/PageRouting/classify.vue"),
      },
      {
        path: "/car",
        name: "car",
        component: () => import("../views/PageRouting/car.vue"),
      },
      {
        path: "/me",
        name: "me",
        component: () => import("../views/PageRouting/me.vue"),
      },
    ],
  },
  {
    path: "/me/information",
    name: "information",
    component: () => import("../views/me/information.vue"),
  },
  {
    path: "/GroupBuyingArea",
    name: "GroupBuyingArea",
    component: () => import("../views/subpage/GroupBuyingArea.vue"),
  },
  {
    path: "/directSupply",
    name: "directSupply",
    component: () => import("../views/subpage/directSupply.vue"),
  },
  {
    path: "/firstPublish",
    name: "firstPublish",
    component: () => import("../views/subpage/firstPublish.vue"),
  },
  {
    path: "/recommend",
    name: "recommend",
    component: () => import("../views/subpage/recommend.vue"),
  },
  {
    path: "/choiceness",
    name: "choiceness",
    component: () => import("../views/subpage/choiceness.vue"),
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/subpage/search.vue"),
  },
  {
    path: "/ulList",
    name: "ulList",
    component: () => import("../views/subpage/ulList.vue"),
  },
  {
    path: "/sonDirectSupply",
    name: "sonDirectSupply",
    component: () => import("../views/sonSubpage/sonDirectSupply.vue"),
  },
  {
    path: "/sonFirstPublish",
    name: "sonFirstPublish",
    component: () => import("../views/sonSubpage/sonFirstPublish.vue"),
  },
  {
    path: "/sonChoiceness",
    name: "sonChoiceness",
    component: () => import("../views/sonSubpage/sonChoiceness.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/login.vue"),
  },
  {
    path: "/myOrder",
    name: "myOrder",
    component: () => import("../views/me/myOrder.vue"),
  },
  {
    path: "/login/registerGetCode",
    name: "registerGetCode",
    component: () => import("../views/login/registerGetCode.vue"),
  },
  {
    path: "/login/registerSubmit/:id",
    name: "registerSubmit",
    component: () => import("../views/login/registerSubmit.vue"),
  },
  {
    path: "/login/forget",
    name: "forget",
    component: () => import("../views/login/forget.vue"),
  },
  {
    path: "/login/forget/reset",
    name: "reset",
    component: () => import("../views/login/reset.vue"),
  },
  {
    path: "/coupon",
    name: "coupon",
    component: () => import("../views/me/coupon.vue"),
  },
  {
    path: "/address",
    name: "address",
    component: () => import("../views/me/address.vue"),
  },
  {
    path: "/collect",
    name: "collect",
    component: () => import("../views/me/collect.vue"),
  },
  {
    path: "/server",
    name: "server",
    component: () => import("../views/me/server.vue"),
  },
  {
    path: "/feedback",
    name: "feedback",
    component: () => import("../views/me/feedback.vue"),
  },
  {
    path: "/help",
    name: "help",
    component: () => import("../views/me/help.vue"),
  },
  {
    path: "/edit",
    name: "edit",
    component: () => import("../views/me/edit.vue"),
  },
];

const router = new VueRouter({
  // mode: 'history',
  mode: "hash",
  routes,
});

// 添加判断
const WhiteList = ["car", "me"];
// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem("token");
  if (token) {
    // 如果存在token，放行
    if (to.name === "login") {
      next(from.path);
    } else {
      next();
    }
  } else {
    // 如果不存在token，跳转到登录页
    // 判断数组里边有没有这一项
    if (WhiteList.includes(to.name)) {
      next("/login");
    } else {
      next();
    }
  }
});
export default router;
